package com.simple.simple_view.controller;

import com.simple.simple_view.domain.ResponseResult;
import com.simple.simple_view.domain.dto.UpdateUserInfoDto;
import com.simple.simple_view.domain.entity.User;
import com.simple.simple_view.domain.enums.AppHttpCodeEnum;
import com.simple.simple_view.exception.SystemException;
import com.simple.simple_view.service.UserService;
import javax.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/*
 *@description: 用户相关功能
 *@author: hisen
 *@Time: 2023/10/26  16:30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        if(!StringUtils.hasText(user.getUsername())) {
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getPassword())) {
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        return userService.register(user);
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUsername()))
        {
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getPassword()))
        {
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        return userService.login(user);
    }

    @PostMapping("/update")
    public ResponseResult update(@ModelAttribute UpdateUserInfoDto updateUserInfoDto) throws Exception {
        if(!StringUtils.hasText(updateUserInfoDto.getUsername()))
        {
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if(!StringUtils.hasText(updateUserInfoDto.getUid()))
        {
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        //rabbitTemplate.convertAndSend("exchange_topic","send_sys","sms");
        return userService.updateUserInfo(updateUserInfoDto);
    }
    @PostMapping("/userInfo")
    public ResponseResult getUserInfo(@RequestParam Long userId){
        if(userId==null)
        {
            throw new SystemException(AppHttpCodeEnum.USER_ID_NOT_NULL);
        }
        return userService.getUserInfo(userId);
    }
}
